package com.cheboohui.iqs.spider.login;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.CloseableHttpClient;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.cheboohui.iqs.common.basic.StringUtil;
import com.cheboohui.iqs.http.tool.HttpClient;

/**
 * 验证平安登录
 * 
 * @author xielonghai
 *
 */
public class VerificationLogin {
	/**
	 * logger:日志工具.
	 */
	private static Log log = LogFactory.getLog(VerificationLogin.class);

	/**
	 * 登录httpClient
	 */
	private static CloseableHttpClient httpClient;
	/**
	 * 登录httpContext
	 */
	private static HttpClientContext httpContext;
	/**
	 * 记录登录平安次数
	 */
	private static int loginNum = 1;

	/**
	 * 记录获取平安验证码次数
	 */
	private static int orderNum = 1;

	private static boolean login = false;

	static {
		log.info("静态登录平安系统..." + goLogin());
	}

	/**
	 * 获取平安验证号
	 * 
	 * @return
	 */
	public static LoginEntity getNo() {
		String orderNo = "";
		if (login)
			orderNo = getOrderNo();
		else {
			log.info("再次登录平安系统..." + goLogin());
		}

		if (StringUtil.isNulls(orderNo)) {
			log.info("再次登录平安系统..." + goLogin());

			if (login)
				orderNo = getOrderNo();

			if (StringUtil.isNulls(orderNo)) {
				log.error("Once Again to Ping An get orderNo is failure !!");
				return new LoginEntity();
			} else {
				log.info("Once Again to Ping An get orderNo is success !!");
				return new LoginEntity(orderNo, httpClient, httpContext);
			}

		} else {
			log.info("Once to Ping An get orderNo is success !!");
			return new LoginEntity(orderNo, httpClient, httpContext);
		}
	}

	/**
	 * 获取平安登录验证号
	 * 
	 * @return
	 */
	private static String getOrderNo() {
		try {
			String url = "https://icorepnbs.pingan.com.cn/icore_pnbs/mainCtrl.tpl?"
					+ "applicantPersonFlag=1&familyPrd=&bsDetailCode=2-3-G-F&usageAttributeCode=02&"
					+ "ownershipAttributeCode=03&insuranceType=1&agentSalerName=&businessCertificateNum=&"
					+ "empBusinessCertificateNum=&deptCodeText=21825&secondLevelDepartmentCode=218&"
					+ "deptCode=21825&employeeCodeText=2180001456&employeeCode=2180001456&channelCode=G&"
					+ "agentCode=18000290&partnerWorknetPanel=218101408008&productCombineList=&autoInsurance=true&"
					+ "propertyInsurance=false&accidentInsurance=false&rateClassFlag=14&worknetCode=&"
					+ "conferVal=1800029014002+8&agentNameLike=&agentCodeText=&brokerCode=&"
					+ "agentName=%E9%87%8D%E5%BA%86%E5%8D%8E%E7%91%9E%E4%BF%9D%E9%99%A9%E4%BB%A3%E7%90%86%E6%9C%89%E9%99%90%E5%85%AC%E5%8F%B8&"
					+ "conferNo=1800029014002&subConferNo=8&dealerCode=&employeeName=%E6%9D%8E%E5%AE%B6%E9%BA%9F&"
					+ "saleGroupCode=21825000349&businessMode=&systemId=ICORE-PTS&applyApproach=";

			String re = HttpClient._httpGetToBody(httpClient, httpContext, url);
			
			String token = "mainQuotationNo = '(.*?)';";
			Pattern p = Pattern.compile(token);
			Matcher m = p.matcher(re);
			while (m.find()) {
				String orderNo = m.group(1);
				log.info("第" + (loginNum - 1) + "次登录平安,第" + orderNum + "次,获取验证号成功:" + orderNo);
				orderNum++;
				return orderNo;
			}
			log.error("第" + (loginNum - 1) + "次登录平安,第" + orderNum + "次,获取验证号失败...");
			login = false;
			return "";
		} catch (Exception e) {
			login = false;
			log.error("第" + (loginNum - 1) + "次登录平安,第" + orderNum + "次,获取验证号异常:" + e);
			return null;
		}
	}

	/**
	 * 平安爬虫登录
	 */
	private static String goLogin() {
		httpClient = HttpClient.createSSLClientDefault();
		httpContext = HttpClientContext.create();

		String msg = "";
		if (Login.login(httpClient, httpContext)) {
			login = true;
			msg = "第" + loginNum + "次,登录平安成功...";
			log.info(msg);
		} else {
			login = false;
			msg = "第" + loginNum + "次,登录平安失败...";
			log.error(msg);
		}
		loginNum++;
		orderNum = 1;
		return msg;
	}
}
